<?php
session_start();
require_once("../config/include_tokyo.inc.php");
require_once("../class/Common_Function.php");
require_once("../class/Core_Lib_Page.php"); //翻页

//访问权限验证
$userRole = $_SESSION['user_role'];
if($userRole == 1 || $userRole == 8){
	//管理员 || 团队成员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}

//get post value
$doFlag = $_POST["doFlag"];
$id = $_POST["id"];
$user_id = $_SESSION['user_id'];

//do actions
if($doFlag == "addSuggestion"){
	//新加 我的建议
	$suggestion_content = $_POST["suggestion_content"];	
	$sql = "INSERT INTO teamsuggestion (user_id,suggestion_content,create_time,suggestion_status) VALUES ($user_id,'$suggestion_content',NOW(),1)";
	$db->Execute($sql);
}else if($doFlag == "deleteSuggestion"){
	//删除 我的建议
	$id = $_POST["id"];
	$sql = "delete from teamsuggestion where id = $id";
	$db->Execute($sql);
}




//get customers list, 翻页
$listFlag = $_POST["listFlag"];
$list_rows = 20; //每页显示的记录数
$sql_all = "SELECT COUNT(*) as total_rows from teamsuggestion where user_id=$user_id"; 

$result = $db->Execute($sql_all);
while (!$result->EOF) {
	$totalRows = $result->fields['total_rows'];
	$result->MoveNext();
}
$nowPage = $_POST['nowPage'];
$nowPage = !empty($nowPage) ? $nowPage : 1;
$params = array(
	'total_rows'=>$totalRows,
	'list_rows'=>$list_rows,
	'method'    =>'ajax',
	'ajax_func_name' =>'goToPage',
	'now_page' => $nowPage 
);
$page = new Core_Lib_Page($params);
$limit_m = $page->first_row;
$limit_n = $list_rows;


$sql_list = "select * from teamsuggestion where user_id=$user_id order by suggestion_status asc,id desc limit $limit_m,$limit_n";

$result = $db->Execute($sql_list);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "我的建议"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>


<?php include("myteamMenu.php"); ?>


<form name="listForm" action="mySuggestion.php" method="post">
<div id="container" >

<div id="newDiary">
<div class="leftDiv">
<table boder=0><tr>
<td><textarea name="suggestion_content" id="suggestion_content" rows=6 cols=60></textarea></td>
<td width=20>&nbsp;</td>
<td><a href='javascript:addSuggestion()' class='button icon add'>提交</a></td>
<td width=20>&nbsp;</td>
<td>
----------------------<br>
1.遇到的问题;<br> 
2.改进意见;<br>
3.想法建议;<br>
----------------------<br>
</td>
</tr></table>
</div>
</div>
<div class="clearBoth">&nbsp;</div>


<div class="grayTable">	
<table >
<thead>
	<tr>
	    <th scope="col" >创建日期</th>
		<th scope="col" >建议内容</th>
		<th scope="col" >建议状态</th>
		<th scope="col" >回复内容</th>
		<th scope="col" >操作</th>
	</tr>
</thead>

<tbody>
	<?php
	while (!$result->EOF) {
		$id = $result->fields['id'];
		$create_time = $result->fields['create_time'];
		$suggestion_content = $result->fields['suggestion_content'];
		$suggestion_content = "<div id='suggestion_content_$id'>$suggestion_content</div>";	
		$suggestion_status = $result->fields['suggestion_status'];
		$actionStr = "未知状态";
		if($suggestion_status == 1){
			$suggestion_status = "处理中";
			$actionStr = "<a href='javascript:doDelete($id)' class='button icon remove'>删除</a>";
		}else if($suggestion_status == 2){
			$suggestion_status = "已回复";
			$actionStr = "存档";
		}
		$suggestion_review = $result->fields['suggestion_review'];
		
		//-------------------------------------------------
		$strTR = "<tr >";
		$strTR = $strTR ."<td>".$create_time ."</td>";
		$strTR = $strTR ."<td>".$suggestion_content ."</td>";
		$strTR = $strTR ."<td>".$suggestion_status."</td>";
		$strTR = $strTR ."<td>".$suggestion_review."</td>";
		$strTR = $strTR ."<td>".$actionStr."</td>";
		$strTR =  $strTR ."</tr >";
		$strTR =  $strTR ."<script> ";
		$strTR =  $strTR ."$('#suggestion_content_$id').html(replaceEnter2BR($('#suggestion_content_$id').html()));";
		$strTR =  $strTR ."</script> ";
		echo $strTR;		
		$result->MoveNext();
	}
	?>
</tbody>

					
</table>

<br>

<!--Page 分页,翻页 -->
<div id="page"><?php echo $page->show(2);?></div>

</div>



</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" >
<input type="hidden" name="id" id="id" >
<input type="hidden" name="nowPage" id="nowPage" value="<?php echo $nowPage; ?>"><!--翻页-->
</form>





</body>




</html>

<script>
/**翻页**/
function goToPage(nowPage){
	document.getElementById('doFlag').value = 'toList';
	document.getElementById('nowPage').value = nowPage;
	document.listForm.submit();
}

/** 添加  **/
function addSuggestion(){
	var suggestion_content = $("#suggestion_content").val();	
	if(suggestion_content==""){
		alert("建议内容不能为空.");
		$("#suggestion_content").focus();  
		return;
	}else{
		document.getElementById('doFlag').value = "addSuggestion";
		document.listForm.submit();
	}
}



/** 删除 **/
function doDelete(id){
	if(confirm("是否删除这条记录,删除后不可恢复,是否继续?")){ 
		document.getElementById('doFlag').value = "deleteSuggestion";
		document.getElementById('id').value = id;
		document.listForm.submit();
	}else{
		return;
	}	
}


</script>

